package com.luuyi.actionchain.action;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;

public class ToastAction extends AbsAction {

    public static final String DEFAULT_TOAST = "Default Toast";

    private String toastContent = DEFAULT_TOAST;
    private Context mContext;

    public ToastAction(Context context) {
        setIsSyncAction(false);
        mContext = context;
    }

    public ToastAction setContent(String content) {
        toastContent = content;
        return this;
    }

    public ToastAction setContext() {
        return this;
    }

    public String getContent() {
        return toastContent;
    }

    @Override
    public void handle() {
        if (TextUtils.isEmpty(toastContent) || mContext == null) {
            doNext();
            return;
        }
        Toast toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
        toast.setText(TextUtils.isEmpty(toastContent) ? DEFAULT_TOAST : toastContent);
        toast.show();
        new Handler(mContext.getMainLooper()).postDelayed(this::doNext, 2000L);
    }
}
